(function() {
	
	if (!window.CJ) {
		window.CJ = {};
	}

	function SubList () {
		this.name = 'SubList';
		this.moveDistance = -208;
		this.pageUnitCount = 4;
	}
	
	SubList.loadAllList = function (cid, pid, eid) {
		CJ.Scene["Channel"].setFocus(cid);
		//channel 에 해당 되는 Program 목록 읽어 오기
		CJ.Program.getData(cid, true, function () {
			CJ.Scene["Program"].setFocus(pid);
			// program 에 해당 되는 episode 목록 읽어 오기
			CJ.Episode.getData(pid, function () {
				CJ.Scene["Episode"].setFocus(eid);
			});
		});
	};
	
	SubList.prototype = new CJ.Horizon();
	SubList.prototype.bindEvent = function () {
		var self = this;
		
		//좌우측 페이징 버튼 Click
		$(this.rootID+ " .btn_prev").on('click', function(){
			//self.extend();
			CJ.Scene["Qmenu"].removeFocus();
			CJ.Scene.setDelegate(CJ.Scene[self.name]);
			CJ.Horizon.prototype.pageUp.call(self);
		});
		$(this.rootID+ " .btn_next").on('click', function(){
			//self.extend();
			CJ.Scene["Qmenu"].removeFocus();
			CJ.Scene.setDelegate(CJ.Scene[self.name]);
			CJ.Horizon.prototype.pageDown.call(self);
		});
		
		//좌우측 페이징 버튼  mouseover
		$(this.rootID+ " .btn_prev").on('mouseenter', function(){
			//self.extend();
			CJ.Scene["Qmenu"].removeFocus();
			CJ.Scene.setDelegate(CJ.Scene[self.name]);
			$('#vod  .list ul li.focus').removeClass('focus');
			$('#vod .scroll li.focus').removeClass('focus');
			$(this).addClass('focus');
		});
		$(this.rootID+ " .btn_next").on('mouseenter', function(){
			//self.extend();
			CJ.Scene["Qmenu"].removeFocus();
			CJ.Scene.setDelegate(CJ.Scene[self.name]);
			$('#vod  .list ul li.focus').removeClass('focus');
			$('#vod .scroll li.focus').removeClass('focus');
			$(this).addClass('focus');
		});

		//list mouseover
		$(this.rootElem).on('mouseenter', 'li', function(){
			//하위 모든 포커스 제거
			CJ.Scene["Qmenu"].removeFocus();
			CJ.Scene.setDelegate(CJ.Scene[self.name]);
			$('#vod .btn.focus').removeClass('focus');
			$(this).siblings().removeClass('select focus');
			$(this).addClass('select focus');
		});
		CJ.Horizon.prototype.bindEvent.call(this);
	};
	SubList.prototype.render = function (lists) {
		var childCount = this.rootElem.children().length;
		var method =  childCount > 0 ? 'append' : 'html';
		$( this.rootElem )[method](
			$.render[this.template]( lists )
		);
		if (method == 'html') {
			var obj = CJ.getStaticObject(this.name);
			var total = obj.totalCount;
			this.rootElem.css({'width': Math.abs(total * this.moveDistance)+1+ 'px', 'left': '0px'});
			$('li', this.rootElem).removeClass('select').eq(0).addClass('select');
		}
	};
	SubList.prototype.extend = function() {
		////Episode.getMore();
		CJ.getStaticObject(this.name).getMore();
	};
	SubList.prototype.setFocus = function(eid) {
		var elem = $('li['+this.did+'="'+eid+'"]', this.rootElem ).get(0);
		CJ.Horizon.prototype.setFocus.call(this, elem);
	};
	SubList.prototype.back = function () {
		$(this.rootID).removeClass('focus');
		$(this.rootID).parent().removeClass('focus');
		CJ.plugin.Player.channeling();
		CJ.Scene.videoPlayBackCB();
	};
	SubList.prototype.up = function() {
		var prev, ctl;
		var now = $(this.rootID + ' .focus');
		if (now && now.length > 0) {
			//Qmenu (VOD) 로 이동
			now.removeClass('focus');
			CJ.Scene.qmenu();
		} else { //카테고리 메뉴 좌측 버튼
			now = $(this.rootID).parent().find('.on');
			prev = now.prev();
			if (prev && prev.length > 0) {
				ctl = prev.data('ctl');
				now.removeClass('on focus').siblings().removeClass('on focus');
				prev.addClass('on focus');
				if (ctl == 'program') {
					CJ.Scene.sub('ProgramSub');
				} else if (ctl == 'channel') {
					CJ.Scene.sub('ChannelSub');
				}
			} else {
				//Qmenu (VOD) 로 이동
				now.removeClass('focus');
				CJ.Scene.qmenu();
			}
		}
	};
	SubList.prototype.down = function() {
		var next, ctl;
		var now = $(this.rootID + ' .focus');
		if (now && now.length > 0) {
		} else { //카테고리 메뉴에서 좌측 버튼
			now = $(this.rootID).parent().find('.on');
			next = now.next();
			if (next && next.length > 0) {
				ctl = next.data('ctl');
				now.removeClass('on focus').siblings().removeClass('on focus');
				next.addClass('on focus');
				if (ctl == 'program') {
					CJ.Scene.sub('ProgramSub');
				} else if (ctl == 'episode') {
					CJ.Scene.sub('EpisodeSub');
				}
			}
		}
	};
	SubList.prototype.left = function() {
		var now = $('#vod div.list .focus');
		if (now.hasClass('btn_next')) {				 			//컨텐츠로
			now.removeClass('focus');
			CJ.Horizon.prototype.entryList.call(this);
		} else if (now.hasClass('select')){					//컨텐츠에서 좌측 버튼
			CJ.Horizon.prototype.left.call(this);
		} else if (now.hasClass('btn_prev')) {					//카테고리 메뉴로
			now.removeClass('focus');
			$(this.rootID).parent().find('li.on').addClass('focus');
		}
	};
	SubList.prototype.right = function () {
		var now = $('#vod div.list .focus');
		if (now.hasClass('on')) {				 			//카테고리 메뉴에서 좌측 버튼
			now.removeClass('focus');
			$(this.rootID + ' .btn_prev').addClass('focus');
		} else if (now.hasClass('btn_prev')){				//컨텐츠로
			now.removeClass('focus');
			CJ.Horizon.prototype.entryList.call(this);
		} else if (now.hasClass('select')) {				//리스트 이동
			CJ.Horizon.prototype.right.call(this);
		}
	};
	CJ.SubList = SubList;
	
	$.extend(CJ.Scene, {
							sub: function(type){
								var staticObj, idx;
								var ctl = (typeof type == 'undefined') ? $('#vod .list ul li.on').data('ctl') : '';
								if (type == 'ChannelSub' || ctl == 'channel') {
									idx = 1;
									staticObj =CJ.Channel;
									type = 'ChannelSub';
								} else if (type == 'ProgramSub' || ctl == 'program') {
									idx = 2;
									staticObj = CJ.Program;
									type = 'ProgramSub';
								} else {
									idx = 3;
									staticObj = CJ.Episode;
									type = 'EpisodeSub'
								}
								//this[type].setFocus(staticObj.lastID);
								$('#vod .list .dep'+idx+ ' .focus').removeClass('focus');
								$(this[type].rootID).parent().removeClass('dep1 dep2 dep3').addClass('dep'+idx)
								.find('ul>li').removeClass('on focus').eq(idx-1).addClass('focus on');
								this.setDelegate(this[type]);
							}
						});


	$(document).ready(function(){
	
		//영상리스트 좌측 메뉴
		$('#vod .list ul li').on('click', function(){
			var ctl = $(this).addClass('on focus').siblings().removeClass('on focus').end().data("ctl");
			if (ctl == 'program') {
				CJ.Scene.sub('ProgramSub');
			} else if (ctl == 'channel') {
				CJ.Scene.sub('ChannelSub');
			} else {
				CJ.Scene.sub('EpisodeSub');
			}
		});
		
		//좌측 메뉴 mouseOver 시 처리
		$('#vod .list ul li').on('mouseenter', function(){
			var ctl = $(this).addClass('on focus').siblings().removeClass('on focus').end().data("ctl");
			var objCtl;
			if (ctl == 'program') {
				objCtl = 'ProgramSub';
			} else if (ctl == 'channel') {
				objCtl = 'ChannelSub';
			} else {
				objCtl = 'EpisodeSub';
			}
			CJ.Scene["Qmenu"].removeFocus();
			CJ.Scene.setDelegate(CJ.Scene[objCtl]);
			$('#vod .btn_prev.focus, #vod .btn_next.focus').removeClass('focus');
			$('#vod .scroll li.focus').removeClass('focus');
			$(this).addClass('focus');
		});

	});

})();